<template>
	<view class="section seckill">
		<view class="section-title">
			<text class="title-text">每日秒杀</text>
			<view class="seckill-time">
				<text class="time-label">距结束</text>
				<text class="time-item">{{seckillTime.hours}}</text>
				<text class="time-separator">:</text>
				<text class="time-item">{{seckillTime.minutes}}</text>
				<text class="time-separator">:</text>
				<text class="time-item">{{seckillTime.seconds}}</text>
			</view>
			<text class="more" @click="navigateToMore">更多 ></text>
		</view>
		<scroll-view scroll-x class="seckill-scroll">
			<view class="seckill-card" v-for="(item, index) in seckillProducts" :key="index" @click="navigateToDetail(item.id)">
				<image :src="item.logo" mode="aspectFill" class="seckill-img"></image>
				<view class="seckill-info">
					<text class="seckill-name">{{item.name}}</text>
					<view class="seckill-price-box">
						<text class="seckill-price">¥{{item.seckillPrice}}</text>
						<text class="original-price">¥{{item.price}}</text>
					</view>
					<view class="seckill-progress">
						<view class="progress-bar">
							<view class="progress-inner" :style="{width: (item.sold/item.stock*100)+'%'}"></view>
						</view>
						<text class="progress-text">已抢{{item.sold}}件</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import {useData} from "/common/useData";

const seckillTime = ref({
	hours: '00',
	minutes: '00',
	seconds: '00'
})

const {modelData:seckillProducts}=useData("/product/search",{size:3})

const navigateToDetail = (id) => {
	uni.navigateTo({
		url: `/pages/detail/view?id=${id}`
	})
}

const navigateToMore = () => {
	uni.navigateTo({
		url: `/pages/product-list/seckill`
	})
}

// 更新秒杀倒计时
const updateSeckillTime = () => {
	const now = new Date()
	const end = new Date()
	end.setHours(23, 59, 59, 999)

	const diff = end - now
	const hours = Math.floor(diff / (1000 * 60 * 60))
	const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
	const seconds = Math.floor((diff % (1000 * 60)) / 1000)

	seckillTime.value = {
		hours: hours.toString().padStart(2, '0'),
		minutes: minutes.toString().padStart(2, '0'),
		seconds: seconds.toString().padStart(2, '0')
	}
}

// 生命周期钩子
onMounted(() => {
	updateSeckillTime()
	setInterval(updateSeckillTime, 1000)
})
</script>

<style>
.section {
	background-color: #FFFFFF;
	padding: 20rpx;
	margin-bottom: 20rpx;
}

.section-title {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.title-text {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
	padding-left: 20rpx;
	border-left: 8rpx solid #D02B29;
}

.more {
	font-size: 24rpx;
	color: #999;
}

.seckill-time {
	display: flex;
	align-items: center;
	margin: 0 20rpx;
}

.time-label {
	font-size: 24rpx;
	color: #666;
	margin-right: 10rpx;
}

.time-item {
	background-color: #D02B29;
	color: #FFFFFF;
	padding: 4rpx 12rpx;
	border-radius: 4rpx;
	font-size: 24rpx;
}

.time-separator {
	color: #D02B29;
	margin: 0 4rpx;
	font-size: 24rpx;
}

.seckill-scroll {
	white-space: nowrap;
}

.seckill-card {
	display: inline-block;
	width: 240rpx;
	margin-right: 20rpx;
	background-color: #F5F5F5;
	border-radius: 10rpx;
	overflow: hidden;
}

.seckill-img {
	width: 240rpx;
	height: 240rpx;
}

.seckill-info {
	padding: 10rpx;
}

.seckill-name {
	font-size: 26rpx;
	color: #333;
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	margin-bottom: 8rpx;
}

.seckill-price-box {
	display: flex;
	align-items: baseline;
	margin-bottom: 8rpx;
}

.seckill-price {
	font-size: 32rpx;
	color: #D02B29;
	font-weight: bold;
	margin-right: 10rpx;
}

.original-price {
	font-size: 24rpx;
	color: #999;
	text-decoration: line-through;
}

.seckill-progress {
	display: flex;
	align-items: center;
}

.progress-bar {
	flex: 1;
	height: 6rpx;
	background-color: #EEEEEE;
	border-radius: 3rpx;
	margin-right: 10rpx;
	overflow: hidden;
}

.progress-inner {
	height: 100%;
	background-color: #D02B29;
	border-radius: 3rpx;
}

.progress-text {
	font-size: 22rpx;
	color: #999;
}
</style> 